package com.znsd.zl.app.ocr.utils;

import com.znsd.zl.app.ocr.entity.IdCardEntity;
import com.znsd.zl.app.ocr.entity.IdCardBackEntity;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

/**
 * <p>
 *     JSON工具类
 * </p>
 *
 * @ClassName IdCardUtils
 * @Description 百度OCR文字识别工具类
 * @Author jaypdd
 * @Date 2020/8/29 15:49
 * @Version v1.0
 */
@Component
public class JsonUtils {

	/**
	 * 封装身份证正面信息
	 * @param json    识别成功的正面信息
	 * @return        返回身份证正面信息对象
	 */
	public IdCardEntity idCardFront(JSONObject json){
		JSONObject result = json.getJSONObject("words_result");
		IdCardEntity idCardEntity = new IdCardEntity();
		String name = words(result,"姓名");
		idCardEntity.setName(name);
		String gender = words(result,"性别");
		idCardEntity.setGender(gender);
		String identityCardNo = words(result,"公民身份号码");
		idCardEntity.setIdentityCard(identityCardNo);
		String birth = words(result,"出生");
		idCardEntity.setBirth(birth);
		return idCardEntity;
	}


	/**
	 * 封装身份证背面信息
	 * @param json    识别成功的背面信息
	 * @return        返回身份证背面信息
	 */
	public IdCardBackEntity idCardBackJson(JSONObject json){
		JSONObject jsonObject2 = json.getJSONObject("words_result");
		IdCardBackEntity idCardBackEntity = new IdCardBackEntity();
		String expiryDate = words(jsonObject2, "失效日期");
		String issuingAuthority = words(jsonObject2, "签发机关");
		String issuingDate = words(jsonObject2, "签发日期");
		idCardBackEntity.setExpiryDate(expiryDate);
		idCardBackEntity.setIssuingAuthority(issuingAuthority);
		idCardBackEntity.setIssuingDate(issuingDate);
		return idCardBackEntity;
	}

	/**
	 * 根据json串获取指定键的值
	 * @param jsonObject    读取到的单个json串信息
	 * @param key           获取值的键
	 * @return              返回证件中的单个用户信息值
	 */
	public String words(JSONObject jsonObject, String key) {
		JSONObject object = jsonObject.getJSONObject(key);
		Object obj = object.get("words");
		return (String) obj;
	}


}
